//
//  ByLoginVC.m
//  mijifen_ios
//
//  Created by mc on 2017/10/23.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByLoginVC.h"

@interface ByLoginVC ()
@property (nonatomic, copy) NSArray *placeAry;
@end

@implementation ByLoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"登陆";
    self.view.backgroundColor = BY_TABLE_COLOR;
    [self createViews];
}

#pragma mark - getter
- (NSArray *)placeAry{
    if (!_placeAry) {
        _placeAry = @[@"用户名",@"密码"];
    }
    return _placeAry;
}

#pragma mark - 创建视图
- (void)createViews{
    UIImageView *logo = [[UIImageView alloc] init];
    [self.view addSubview:logo];
    [logo mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(50);
        }else{
            make.top.equalTo(self.view.mas_top).offset(50);
        }
        make.left.equalTo(self.view.mas_left).offset((SCREEN_WIDTH - 100) / 2);
        make.size.mas_equalTo(CGSizeMake(100, 70));
    }];
    logo.backgroundColor = [UIColor redColor];
    
    UILabel *logoLabel = [[UILabel alloc] init];
    [self.view addSubview:logoLabel];
    [logoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(logo.mas_bottom).offset(15);
        make.left.equalTo(self.view.mas_left);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 30));
    }];
    logoLabel.text = @"商户版";
    logoLabel.font = FONT(24);
    logoLabel.textAlignment = NSTextAlignmentCenter;

    
    for (int i = 0; i < self.placeAry.count; i ++) {
        UIView *bgView = [[UIView alloc] init];
        bgView.tag = 10 + i;
        bgView.backgroundColor = [UIColor whiteColor];
        bgView.layer.cornerRadius = 4.0;
        [self.view addSubview:bgView];
        CGFloat y = 30 + 50 * i + 20 * i;
        [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view.mas_left).offset(20);
            make.top.equalTo(logoLabel.mas_bottom).offset(y);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 40, 50));
        }];
        UITextField *textField = [[UITextField alloc] init];
        [bgView addSubview:textField];
        [textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(bgView.mas_left);
            make.top.equalTo(bgView.mas_top).offset(10);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 40, 30));
        }];
        textField.placeholder = self.placeAry[i];
        textField.font = FONT(16);
        textField.textAlignment = NSTextAlignmentCenter;
        textField.tag = 100 + i;
    }
    
    UIView *bgViewLast = [self.view viewWithTag:11];
    UIButton *loginButton = [[UIButton alloc] init];
    [self.view addSubview:loginButton];
    [loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(20);
        make.top.equalTo(bgViewLast.mas_bottom).offset(40);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 40, 50));
    }];
    [loginButton setTitle:@"登陆" forState:UIControlStateNormal];
    [loginButton setTitleColor:BY_TEXT_COLOR forState:UIControlStateNormal];
    loginButton.titleLabel.font = FONT(18);
    loginButton.backgroundColor = BY_THEME_COLOR;
    loginButton.layer.cornerRadius = 4.0;
    [loginButton addTarget:self action:@selector(loginAction) forControlEvents:UIControlEventTouchUpInside];

}

#pragma mark - 登陆
- (void)loginAction{
    UIView *userNameBgView = [self.view viewWithTag:10];
    UIView *passwordBgView = [self.view viewWithTag:11];
    UITextField *userTf = [userNameBgView viewWithTag:100];
    UITextField *nameTf = [passwordBgView viewWithTag:101];
    if (userTf.text.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请输入用户名"];
        return;
    }
    if (nameTf.text.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请输入密码"];
        return;
    }
    // todo: 待接口接入 成功调用
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    window.rootViewController = [[ByTabBrController alloc] init];

}



@end
